package com.champChooser.panels.book
{
	//http://blog.flexexamples.com/2007/10/11/creating-clustered-stacked-overlaid-and-100-bar-charts-in-flex-3/
	import com.champChooser.constants.CCConstants;
	
	import flash.events.Event;
	
	import mx.charts.PieChart;
	import mx.charts.events.ChartItemEvent;
	import mx.collections.ArrayCollection;
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	public class BookDemoPanel extends Panel
	{
	public const statServiceURL:String = "http://"+CCConstants.SERVER_IP+"/ChampChooser/core/services/player/playerBulkStatService.php";
	public const performanceServiceURL:String = "http://"+CCConstants.SERVER_IP+"/ChampChooser/core/services/player/playerBulkPerformanceService.php";
	public  var statService:HTTPService;
	public  var performancesService:HTTPService;
	public  var bulkStatService:HTTPService;
	[Bindable]
	public var stats:ArrayCollection;
	[Bindable]
	public var performances:ArrayCollection;
	[Bindable]
	public var runs:ArrayCollection;
	[Bindable]
	public var wickets:ArrayCollection; 
	[Bindable]
	public var performanceCollection:ArrayCollection; 
	[Bindable]
	public var fieldingStats:ArrayCollection;
	[Bindable]
	public var bulkStats:ArrayCollection;
	[Bindable]
	public var runs1:ArrayCollection;
	[Bindable]
	public var runs2:ArrayCollection;
	[Bindable]
	public var batsman1Name:String;
	[Bindable]
	public var batsman2Name:String;
	
	
	public var batttingStatsChart:PieChart;
	public function BookDemoPanel()
	{
		super();
	}
	
	public function getBulkStats(event:ResultEvent):void{
		bulkStats = event.result.records.stat as ArrayCollection;
		if(bulkStats.length>2){
			Alert.show("Not allowed");
		}else{
			generateComparisonBattingStats(bulkStats);
		}
	}
	
	
	public function getStats(event:ResultEvent):void{
		stats = event.result.records.stat.record as ArrayCollection;
		getBattingStats(stats);
		getBowlingStats(stats);
		getWicketStats(stats);
	}
	public function getPerformances(event:ResultEvent):void{
		performances = event.result.records.record as ArrayCollection;
		getperformanceChart1(performances);
	}
	public function getperformanceChart1(performances:ArrayCollection):void{
		performanceCollection= new ArrayCollection();
		for(var i:int=0;i<performances.length;i++){
			var o:Object = new Object();
			o.matchID="Match"+(i+1).toString();
			o.runs_scored=performances[i].runs_scored;
			o.balls_faced=performances[i].balls_faced;
			o.sixes=performances[i].sixes;
			o.fours=performances[i].fours;
			o.balls_bowled=performances[i].balls_bowled;
			o.maidens=performances[i].maidens;
			o.wickets=performances[i].wickets;
			o.runs_conceded=performances[i].runs_conceded;
			o.wides=performances[i].wides;
			o.noballs=performances[i].noballs;
			o.catches=performances[i].catches;
			o.stumpings=performances[i].stumpings;
			o.byes=performances[i].byes;
			o.runouts=performances[i].runouts;
			performanceCollection.addItem(o);
		}
	}
	protected function faultHandler(event:FaultEvent):void{
		Alert.show("OOPS");
	}
	public function loadStuff(event:Event):void{
		var params:Object = {};
		params["playerIds"]="214";
		statService.send(params);
		var params1:Object = {};
		params1["playerIds"]="222";
		performancesService.send(params1);
	}
	
	public function loadStuff2(event:Event):void{
		var params2:Object = {};
		params2["playerIds"]="214,70";
		bulkStatService.send(params2);					
	}
	public function generateComparisonBattingStats(stats:ArrayCollection):void{
		batsman1Name=stats[0].record[0].playerId;
		runs1= new ArrayCollection([
        {Runs1:"T20", total:stats[0].record[0].runs,name:"Chetan"  } ,
        {Runs1:"ODI", total:stats[0].record[1].runs,name:"Chetan"  },
        {Runs1:"TEST", total:stats[0].record[2].runs,name:"Chetan" },
		]);
		batsman2Name=stats[1].record[0].playerId;
		runs2= new ArrayCollection([
        {Runs1:"T20", total:stats[1].record[0].runs,name:"Chetan" } ,
        {Runs1:"ODI", total:stats[1].record[1].runs ,name:"Chetan"},
        {Runs1:"TEST", total:stats[1].record[2].runs ,name:"Chetan"},
		]);
	}
	
	public function getBattingStats(stats:ArrayCollection):void{
		runs= new ArrayCollection([
        {Runs1:"T20", total:stats[0].runs + (stats[0].fours*4) + (stats[0].sixes*6)} ,
        {Runs1:"ODI", total:stats[1].runs + (stats[1].fours*4) + (stats[1].sixes*6)},
        {Runs1:"TEST", total:stats[2].runs +(stats[2].fours*6) + (stats[2].sixes*6)},
		]);
	}
	public function drillDown(event:ChartItemEvent):void{
		var ac:Object = event.hitData.chartItem.item;
		var index:int=0;
		if(ac.Runs1=="T20"){
			index=0;
			Panel(batttingStatsChart.parent).title="T20 Stats";
		}else if(ac.Runs1=="ODI"){
			index = 1;
			Panel(batttingStatsChart.parent).title="ODI Stats";
		}else {
			index = 2;
			Panel(batttingStatsChart.parent).title="TEST Stats";
		}
		runs = new ArrayCollection([
        {Runs1:"Others", total:stats[index].runs},
        {Runs1:"Fours", total:(stats[index].fours*4)},
        {Runs1:"Sixes", total:(stats[index].sixes*6)},
		]);
		batttingStatsChart.removeEventListener(ChartItemEvent.ITEM_CLICK,drillDown);
		batttingStatsChart.addEventListener(ChartItemEvent.ITEM_CLICK,drillUp);
		batttingStatsChart.legendData.label= "5643";
	}
	public function drillUp(event:ChartItemEvent):void{
		runs= new ArrayCollection([
        {Runs1:"T20", total:stats[0].runs + (stats[0].fours*4) + (stats[0].sixes*6)} ,
        {Runs1:"TESTS", total:stats[1].runs + (stats[1].fours*4) + (stats[1].sixes*6)},
        {Runs1:"ODI", total:stats[2].runs +(stats[2].fours*6) + (stats[2].sixes*6)},
		]);
		batttingStatsChart.removeEventListener(ChartItemEvent.ITEM_CLICK,drillUp);
		batttingStatsChart.addEventListener(ChartItemEvent.ITEM_CLICK,drillDown);
		Panel(batttingStatsChart.parent).title="Batting Stats";
	}
	public function getBowlingStats(stats:ArrayCollection):void{
		wickets = new ArrayCollection([
        {Wickets1:"Others", total:stats[0].wickets},
        {Wickets1:"5 Wickets", total:(stats[0].fivewickets*5)},
        {Wickets1:"10 Wickets", total:(stats[0].tenwickets*10)},
		]);
	}
	
	public function getWicketStats(stats:ArrayCollection):void{
		fieldingStats = new ArrayCollection([
        {Fieldings1:"RunOuts", total:stats[0].runouts},
        {Fieldings1:"Catches", total:stats[0].catches},
        {Fieldings1:"Stumpings", total:stats[0].stumpings},
		]);
	}
	}
}